package com.qf.mvc02.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {

    /**
     * DispatcherServlet会优先调用拦截器的 preHandle 方法
     * @param request
     * @param response
     * @param handler
     * @return 如果返回false，那么请求到此为止，不会向后面的controller发送了  ； 返回true ，会让请求继续发送给后续的 controller
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 请求参数校验工作
        System.out.println("========================================");
        return true;
    }

    /**
     * DispatcherServlet会在调用完controller中的方法后，再次调用 postHandle 方法
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    /**
     * DispatcherServlet会确保一定会调到该方法，无论请求的处理过程中是否抛出异常。
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("！！！！！！！！！！！！！！！！！！！！！！！！！！！");
    }
}
